﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Primitives;
using Microsoft.Windows.Controls;
using System.Windows.Data;
namespace WpfApplicationExperimental
{


    public class MainDatagridColumnsGenerator
    {


        //polymorphism for this i dnt know  ? maybe for extensibility latter 
        //this ifs is bad design 

        /// <summary>
        /// this is a programmatic column generation . maybe even visitor // refactoring to patterns later 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static  IList<DataGridColumn> GetDataGridColumns(string  type)
        {
            IList<DataGridColumn> columns = new List<DataGridColumn>();


            if(type .Contains ( "Document"))
            {

                System .Collections .IEnumerable docTypes =  new DataAccesLayer.EQualDataLayerFactory ().GetDocumentAccesLayer ().GetDocumentTypes();

                columns.Add(GetGridTextColumn("Id"));
                columns.Add(GetGridTextColumn("Title"));
                columns.Add(GetDataGridComboBoxColumn(docTypes, "name", "id", "Type"));
                columns.Add(GetDataGridComboBoxColumn(docTypes, "id", "id", "Type"));


            }

            if (type .Contains ( "Employee"))
            {
                DataGridTextColumn id_c = new DataGridTextColumn();
                id_c.Header = "Id"; id_c.Binding = new Binding("id");
                columns.Add(id_c);

                DataGridTextColumn id_n = new DataGridTextColumn();
                id_n.Header = "Name"; id_n.Binding = new Binding("name");
                columns.Add(id_n);

                DataGridTextColumn id_s = new DataGridTextColumn();
                id_s.Header = "Surname"; id_s.Binding = new Binding("surname"); 
                columns.Add(id_s);

            }
            return columns;
        }

        #region Textbox columns

        public static DataGridTextColumn GetGridTextColumn(string property, string Header)
        {
            DataGridTextColumn column = new DataGridTextColumn();
            column . Header = Header; column . Binding = getSimpleBinding(property);
            return column;
        }

        public static DataGridTextColumn GetGridTextColumn(string property)
        {
            DataGridTextColumn column = GetGridTextColumn(property, property);
            return column;
        }

        public static Binding getSimpleBinding(string property)
        {
            return new Binding(property);
        }

        #endregion 

        #region combobox columns

        public static DataGridComboBoxColumn GetDataGridComboBoxColumn(System .Collections .IEnumerable itemsSource, string displayMember, string Header,string selectedvalue,string propery)
        {
            DataGridComboBoxColumn type_c = new DataGridComboBoxColumn();
            type_c.DisplayMemberPath = displayMember ; type_c.Header = Header;
            type_c.ItemsSource = itemsSource;
            type_c.SelectedValuePath = selectedvalue;
            type_c.SelectedValueBinding = getSimpleBinding(propery);
            return type_c;
        }

        public static DataGridComboBoxColumn GetDataGridComboBoxColumn(System.Collections.IEnumerable itemsSource, string displayMember,string selectedvalue, string propery)
        {
            DataGridComboBoxColumn type_c = new DataGridComboBoxColumn();
            type_c.DisplayMemberPath = displayMember; type_c.Header = propery ;
            type_c.ItemsSource = itemsSource;
            type_c.SelectedValuePath = selectedvalue;
            type_c.SelectedValueBinding = getSimpleBinding(propery);
            return type_c;
        }

        #endregion 

        //this obviously belong to secondary whatever datagrid
        //polymorpish this .string is not the case form parameter 
        public static IList<DataGridColumn> GetDataGridColumnsNewItemSecondaryGrid(string type)
        {
            IList<DataGridColumn> columns = new List<DataGridColumn>();
            if (type.Contains("Employee"))
            {
                IEnumerable <DataAccesLayer .Employee > empls = new DataAccesLayer.EmployeeDataAccesLayer().GetEmployees();
                DataGridComboBoxColumn type_cId = new DataGridComboBoxColumn();
                type_cId.DisplayMemberPath = "id"; type_cId.Header = "Id";
                type_cId.ItemsSource = empls;
                type_cId.SelectedValuePath = "id";
                type_cId.SelectedValueBinding = new Binding("id");
                columns.Add(type_cId);

                DataGridComboBoxColumn type_c = new DataGridComboBoxColumn();
                type_c.DisplayMemberPath = "name"; type_c.Header = "Name";
                type_c.ItemsSource = empls;
                type_c.SelectedValuePath = "id";
                type_c.SelectedValueBinding = new Binding("id");
                columns.Add(type_c);

                DataGridComboBoxColumn surname_c = new DataGridComboBoxColumn();
                surname_c.DisplayMemberPath = "surname"; surname_c.Header = "SurName";
                surname_c.ItemsSource = empls;
                surname_c.SelectedValuePath = "id";
                surname_c.SelectedValueBinding = new Binding("id");
                columns.Add(surname_c);

            }

            if (type.Contains("Form"))
            {
                DataGridComboBoxColumn type_cId = new DataGridComboBoxColumn();
                type_cId.DisplayMemberPath = "FormId"; type_cId.Header = "Id";
                type_cId.ItemsSource = new DataAccesLayer.DocumentDataAccesLayer().GetFormsAndRecords();
                type_cId.SelectedValuePath = "FormId";
                type_cId.SelectedValueBinding = new Binding("FormId");
                columns.Add(type_cId);

                DataGridComboBoxColumn type_c = new DataGridComboBoxColumn();
                type_c.DisplayMemberPath = "Title"; type_c.Header = "Title";
                type_c.ItemsSource = new DataAccesLayer.DocumentDataAccesLayer().GetFormsAndRecords();
                type_c.SelectedValuePath = "FormId";
                type_c.SelectedValueBinding = new Binding("FormId");
                columns.Add(type_c);

            }

            return columns;
        }

        internal static IEnumerable<DataGridColumn> GetDataGridColumnsFiltered(string p, List<string> list)
        {

            var cols = from c in GetDataGridColumns(p)
                       from s in list
                       where c.Header.ToString ().ToUpper ()== s.ToString ().ToUpper ()
                       select c;

            object collist =  cols.ToList();
            return collist as IEnumerable<DataGridColumn> ;
            
        }
    }

  


    public class MainDatagridColumnVisitor 
    {

        #region IVisitor Members

        public void VisitBaseDao(DataAccesLayer.DomainModel.BaseDataAccesObject dao)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
